// 分布统计，车辆-总数统计，概况
import i18n from '@/lang'
export default {
  initOptions: {
    tooltip: {
      trigger: 'item',
      formatter: '{a} <br/>{b}: {c} ({d}%)'
    },
    legend: {
      orient: 'vertical',
      width: 10,
      top: 0,
      left: 0,
      data: [],
      formatter (name) {
        return name.length > 9 ? name.substr(0, 9) + '...' : name
      }
    },
    series: [{
      name: '',
      type: 'pie',
      avoidLabelOverlap: false,
      center: ['50%', '50%'],
      label: {
        show: false
      },
      data: []
    }]
  },
  setEchart (echartObject, result, legend, color = '#000') {
    echartObject.setOption(this.initOptions)
    const sortResult = JSON.parse(JSON.stringify(result))
    const seriesResult = []
    const colors = ['#458B74', '#66CDAA', '#CD6839', '#008B8B', '#2F4F4F', '#DC143C', '#91C7AE', '#22B14C', '#E28647']
    for (let i = 0; i < sortResult.length; i++) {
      if (sortResult[i].name === 'other') {
        seriesResult.push({
          value: sortResult[i].value,
          name: i18n.t('others'),
          itemStyle: {
            color: colors[i]
          }
        })
        legend[i] = i18n.t('others')
      } else {
        seriesResult.push({
          value: sortResult[i].value,
          name: sortResult[i].name,
          itemStyle: {
            color: colors[i]
          }
        })
      }
    }
    echartObject.setOption({
      legend: {
        data: legend,
        textStyle: {
          color
        }
      },
      series: [{
        name: i18n.t('version'),
        data: seriesResult
      }]
    })
    setTimeout(() => {
      echartObject.resize()
    })
  }
}
